package com.gardenguell.salad.ui.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.*;
import android.widget.EditText;
import com.gardenguell.salad.R;
import com.gardenguell.salad.data.model.Box;
import com.gardenguell.salad.data.model.History;
import com.gardenguell.salad.ui.activity.AddHistoryActivity;
import com.gardenguell.salad.ui.activity.BoxActivity;
import com.gardenguell.salad.ui.activity.MainActivity;
import com.gardenguell.salad.ui.activity.base.ToolbarActivity;
import com.gardenguell.salad.ui.adapter.BoxAdapter;
import com.gardenguell.salad.ui.adapter.DividerLineDecoration;
import com.gardenguell.salad.ui.adapter.HistoryAdapter;
import com.gardenguell.salad.util.DisplayUtil;
import com.gardenguell.salad.util.GetPhotoManager;
import com.gardenguell.salad.util.ImageLoaderHelper;
import com.gardenguell.salad.util.ResUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by absurd on 2015/11/21.
 */
public class HistoryFragment extends BaseFragment {

    private RecyclerView mHistoryList;
    private HistoryAdapter mHistoryAdapter;

    private GetPhotoManager mGetPhotoManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_history, container, false);
        ((ToolbarActivity)getBaseActivity()).showToolbar();
        ((BoxActivity) getBaseActivity()).setToolbarTitle(ResUtil.getString(R.string.title_activity_add_history));
        mGetPhotoManager = new GetPhotoManager(HistoryFragment.this.getBaseActivity());

        mHistoryList = (RecyclerView)root.findViewById(R.id.history_list);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getBaseActivity());
        mHistoryList.setLayoutManager(layoutManager);
        // set every item's height is fixed
        mHistoryList.setHasFixedSize(true);
        List<History> data = new ArrayList<>();
        for(int i = 0; i < 8; i++)
            data.add(new History());
        mHistoryAdapter = new HistoryAdapter(data);
        mHistoryList.setAdapter(mHistoryAdapter);
        DividerLineDecoration dividerLine = new DividerLineDecoration();
        dividerLine.setSize(DisplayUtil.dip2px(this.getBaseActivity(),10));
        dividerLine.setColor(ResUtil.getColor(R.color.main_background));
        mHistoryList.addItemDecoration(dividerLine);

        root.findViewById(R.id.add_history).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                mGetPhotoManager.chooseFromCamera(10);
                //Intent intent = new Intent(HistoryFragment.this.getBaseActivity(), AddHistoryActivity.class);
                //startActivity(intent);
            }
        });
        return root;
    }


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_history, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_add_history:
                startActivity(new Intent(this.getBaseActivity(), AddHistoryActivity.class));
            break;
        }
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode != Activity.RESULT_OK)
            return;
        mGetPhotoManager.hanlderActivityCameraResult(requestCode, resultCode, data, new GetPhotoManager.CamearListener() {
            @Override
            public void onChooserCamera(String imagePath) {
                Intent intent = new Intent(HistoryFragment.this.getBaseActivity(), AddHistoryActivity.class);
                intent.putExtra("imagePath", imagePath);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            default:
                break;
        }
    }
}
